//
//  PhotoBrowserCollectionViewCell.swift
//  XLWB
//
//  Created by 郭亮 on 16/3/22.
//  Copyright © 2016年 gl. All rights reserved.
//

import UIKit
import SDWebImage

class PhotoBrowserCollectionViewCell: UICollectionViewCell {
    // MARK:-属性
    private lazy var scrollV: UIScrollView = UIScrollView()
    lazy var imageV: UIImageView = UIImageView()
    private lazy var progressView: PhotoBrowserProgressView = PhotoBrowserProgressView()
    var pictureUrl: NSURL? {
        didSet {
            guard let pictureUrl = pictureUrl else {
            return
            }
            //设置cell内容
            setupContent(pictureUrl)
        }
    }
    
    // MARK:-构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        //设置UI界面
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK:-UI界面
extension PhotoBrowserCollectionViewCell {
    private func setupUI() {
        //添加子控件
        addSubview(scrollV)
        scrollV.addSubview(imageV)
        addSubview(progressView)
        //设置frame
        scrollV.frame = contentView.bounds
        scrollV.frame.size.width -= 20
        progressView.frame.size = CGSizeMake(60, 60)
        progressView.center = CGPointMake(scrollV.bounds.width * 0.5, scrollV.bounds.height * 0.5)
        //设置属性
        progressView.hidden = true
        progressView.backgroundColor = UIColor.clearColor()
        scrollV.delegate = self
        scrollV.minimumZoomScale = 0.5
        scrollV.maximumZoomScale = 2.0
    }
    
    private func setupContent(pictureUrl: NSURL) {
        //取出图片
        guard let picture = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(pictureUrl.absoluteString) else {
            return
        }
        //根据图片计算imageV大小
        let scrollVW = scrollV.frame.width
        let scrollVH = scrollV.frame.height
        let imageVW = scrollVW
        let imageVH = imageVW / picture.size.width * picture.size.height
        if imageVH <= scrollVH {
            imageV.frame = CGRectMake(0, (scrollVH - imageVH) * 0.5, imageVW, imageVH)
        }else {
            imageV.frame = CGRectMake(0, 0, imageVW, imageVH)
        }
        imageV.backgroundColor = UIColor.redColor()
        //设置内容尺寸
        scrollV.contentSize = CGSizeMake(0, imageVH)
        //设置图片
        let middleUrl = getMiddlePictureUrl(pictureUrl)
        progressView.hidden = false
        imageV.sd_setImageWithURL(middleUrl, placeholderImage: picture, options: [], progress: { (current, total) -> Void in
            self.progressView.progress = CGFloat(current) / CGFloat(total)
            }) { (_, _, _, _) -> Void in
               self.progressView.hidden = true
        }
    }
    
    private func getMiddlePictureUrl(smallUrl: NSURL) -> NSURL {
        //大图
        let smallString = smallUrl.absoluteString
        let middleString = smallString.stringByReplacingOccurrencesOfString("thumbnail", withString: "bmiddle")
        return NSURL(string: middleString)!
    }
}

// MARK:-scrollView代理方法
extension PhotoBrowserCollectionViewCell: UIScrollViewDelegate {
    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return imageV
    }
    
    func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat) {
        //更改缩放view的frame
        if view?.frame.height > scrollV.frame.height {
        return
        }else {
        view?.center = CGPointMake(scrollV.frame.width * 0.5, scrollV.frame.height * 0.5)
        }
    }
}
